﻿namespace Cmn.Mis
{
    using Cmn;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class UI
    {
        public static void FieldDropDownList(DropDownList dropDownList, string strSql, string valueField, string textField)
        {
            dropDownList.DataSource = DB.getDataTable(strSql);
            dropDownList.DataValueField = valueField;
            dropDownList.DataTextField = textField;
            dropDownList.DataBind();
        }

        public static void FieldDropDownList(DropDownList dropDownList, string strSql, string valueField, string textField, bool IsAllowEmpty)
        {
            FieldDropDownList(dropDownList, strSql, valueField, textField);
            if (IsAllowEmpty)
            {
                dropDownList.Items.Insert(0, new ListItem("", ""));
            }
        }

        public static void FieldDropDownList(DropDownList dropDownList, string connectStr, string strSql, string valueField, string textField)
        {
            dropDownList.DataSource = DB.getDataTable(connectStr, strSql);
            dropDownList.DataValueField = valueField;
            dropDownList.DataTextField = textField;
            dropDownList.DataBind();
        }

        public static void FieldDropDownList(DropDownList dropDownList, string connectStr, string strSql, string valueField, string textField, bool IsAllowEmpty)
        {
            FieldDropDownList(dropDownList, connectStr, strSql, valueField, textField);
            if (IsAllowEmpty)
            {
                dropDownList.Items.Insert(0, new ListItem("", ""));
            }
        }

        public static HtmlForm FindHtmlForm(Control control)
        {
            if (control is HtmlForm)
            {
                return (HtmlForm) control;
            }
            if (control.HasControls())
            {
                HtmlForm form = null;
                foreach (Control control2 in control.Controls)
                {
                    if (control2 is HtmlForm)
                    {
                        return (HtmlForm) control2;
                    }
                    form = FindHtmlForm(control2);
                    if (form != null)
                    {
                        return form;
                    }
                }
            }
            return null;
        }

        public static HtmlForm FindHtmlForm(ControlCollection controlCollection)
        {
            HtmlForm form = null;
            for (int i = 0; i < controlCollection.Count; i++)
            {
                form = FindHtmlForm(controlCollection[i]);
                if (form != null)
                {
                    return form;
                }
            }
            return null;
        }

        public static Label GetBlank(int blankCount)
        {
            Label label = new Label();
            for (int i = 0; i < blankCount; i++)
            {
                label.Text = label.Text + "&nbsp;";
            }
            return label;
        }

        public static Label GetBR(int brCount)
        {
            Label label = new Label();
            for (int i = 0; i < brCount; i++)
            {
                label.Text = label.Text + "<br />";
            }
            return label;
        }

        public static void GetDescList(Dictionary<string, string> valueList, Control control)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        GetDescList(valueList, control2);
                    }
                    else if ((control2.ID != null) && (control2 is DropDownList))
                    {
                        valueList.Add((control2 as DropDownList).DataTextField.ToLower(), (control2 as DropDownList).SelectedItem.Text);
                    }
                }
            }
        }

        public static void GetDetailControl(List<DetailControl> detailControls, Control control)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        GetDetailControl(detailControls, control2);
                    }
                    else if ((control2.ID != null) && (control2 is DetailControl))
                    {
                        detailControls.Add(control2 as DetailControl);
                    }
                }
            }
        }

        public static string GetValue(Control control)
        {
            if (control is TextBox)
            {
                return (control as TextBox).Text;
            }
            if (control is CheckBox)
            {
                return ((control as CheckBox).Checked ? "1" : "0");
            }
            if (control is RadioButton)
            {
                return ((control as RadioButton).Checked ? "1" : "0");
            }
            if (control is RadioButtonList)
            {
                return (control as RadioButtonList).SelectedValue;
            }
            if (control is DropDownList)
            {
                return (control as DropDownList).SelectedValue;
            }
            if (control is HiddenField)
            {
                return (control as HiddenField).Value;
            }
            if (control is BasControl)
            {
                return (control as BasControl).GetValue();
            }
            return null;
        }

        public static void GetValueList(Dictionary<string, string> valueList, Control control)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        GetValueList(valueList, control2);
                    }
                    else if (control2.ID != null)
                    {
                        valueList.Add(control2.ID.Substring(3).ToLower(), GetValue(control2));
                    }
                }
            }
        }

        public static void InitControl(Control control)
        {
            if (control is TextBox)
            {
                if (control.ID.ToLower().Substring(0, 3) == "dte")
                {
                    (control as TextBox).Text = Date.ToDateStr(DateTime.Now);
                }
                else
                {
                    (control as TextBox).Text = "";
                }
            }
            else if (control is CheckBox)
            {
                (control as CheckBox).Checked = false;
            }
            else if (control is RadioButton)
            {
                (control as RadioButton).Checked = false;
            }
            else if (control is RadioButtonList)
            {
                (control as RadioButtonList).SelectedIndex = 0;
            }
            else if (control is DropDownList)
            {
                if ((control as DropDownList).Items.Count > 0)
                {
                    (control as DropDownList).SelectedIndex = 0;
                }
            }
            else if (control is HiddenField)
            {
                (control as HiddenField).Value = "";
            }
            else if (control is BasControl)
            {
                (control as BasControl).InitControl();
            }
            else if (control is DetailControl)
            {
                (control as DetailControl).InitControl();
            }
        }

        public static void InitControls(Control control)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        InitControls(control2);
                    }
                    else
                    {
                        InitControl(control2);
                    }
                }
            }
        }

        public static void SetAllEnabled(Control control, bool value)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        SetAllEnabled(control2, value);
                    }
                    else if (control2.ID != null)
                    {
                        SetEnabled(control2, value);
                    }
                }
            }
        }

        public static int SetControlJs(string tableName, Control control)
        {
            if (!control.HasControls())
            {
                return 0;
            }
            int num = 0;
            //string regexContent = "";
            //string errorMsg = "";
            foreach (Control control2 in control.Controls)
            {
                if ((control2 is PlaceHolder) || (control2 is Panel))
                {
                    num += SetControlJs(tableName, control2);
                }
                //else if ((control2.ID != null) && (FieldCheck.GetRegex(tableName, control2.ID, ref regexContent, ref errorMsg) == 0))
                //{
                //    if (control2 is TextBox)
                //    {
                //        regexContent = regexContent.Replace(@"\", @"\\");
                //        StringBuilder builder = new StringBuilder();
                //        builder.Append("return CheckField(this,'" + regexContent + "','" + errorMsg + "');");
                //        ((TextBox) control2).Attributes.Add("onblur", builder.ToString());
                //        StringBuilder builder2 = new StringBuilder();
                //        builder2.Append("OnFocusIn(this);");
                //        ((TextBox) control2).Attributes.Add("onfocus", builder2.ToString());
                //        num++;
                //    }
                //    else
                //    {
                //        Log.Write(control2.ID + " 不是TextBox，输入规则不能运用！");
                //    }
                //}
            }
            return num;
        }

        public static void SetControlValue(Dictionary<string, string> valueList, Control control)
        {
            if (control.HasControls())
            {
                foreach (Control control2 in control.Controls)
                {
                    if ((control2 is PlaceHolder) || (control2 is Panel))
                    {
                        SetControlValue(valueList, control2);
                    }
                    else
                    {
                        string str;
                        if ((control2.ID != null) && valueList.TryGetValue(control2.ID.Substring(3).ToLower(), out str))
                        {
                            SetValue(control2, str);
                        }
                    }
                }
            }
        }

        public static void SetEnabled(Control control, bool value)
        {
            if (control is TextBox)
            {
                (control as TextBox).Enabled = value;
            }
            else if (control is CheckBox)
            {
                (control as CheckBox).Enabled = value;
            }
            else if (control is RadioButton)
            {
                (control as RadioButton).Enabled = value;
            }
            else if (control is RadioButtonList)
            {
                (control as RadioButtonList).Enabled = value;
            }
            else if (control is DropDownList)
            {
                (control as DropDownList).Enabled = value;
            }
            else if (control is CheckBoxList)
            {
                (control as CheckBoxList).Enabled = value;
            }
            else if (control is FileUpload)
            {
                (control as FileUpload).Enabled = value;
            }
        }

        public static void SetValue(Control control, string value)
        {
            if (value != null)
            {
                if (control is TextBox)
                {
                    (control as TextBox).Text = value;
                }
                else if (control is CheckBox)
                {
                    (control as CheckBox).Checked = value == "1";
                }
                else if (control is RadioButton)
                {
                    (control as RadioButton).Checked = value == "1";
                }
                else if (control is RadioButtonList)
                {
                    try
                    {
                        (control as RadioButtonList).SelectedValue = value;
                    }
                    catch
                    {
                    }
                }
                else if (control is DropDownList)
                {
                    try
                    {
                        (control as DropDownList).SelectedValue = value;
                    }
                    catch
                    {
                    }
                }
                else if (control is HiddenField)
                {
                    (control as HiddenField).Value = value;
                }
                else if (control is BasControl)
                {
                    (control as BasControl).SetValue(value);
                }
            }
        }

        public static void SetVisible(Control control, bool value)
        {
            control.Visible = value;
        }
    }
}

